<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  
  <script type="text/javascript">
    /*
      基于Promise发送Ajax请求
    */
    function requestData(url) {
      const pmse1 = new Promise(function(resolve, reject) {
        const xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function() {
          if(xhr.readyState != 4) return;
          if(xhr.readyState == 4 && xhr.status == 200) {
            // 处理正常的情况
            resolve(xhr.responseText);
          }else{
            // 处理异常情况
            reject('服务器错误');
          }
        };
        xhr.open('get', url);
        xhr.send(null);
      });
      return pmse1;
    }

    //发送一个ajax请求
    requestData('http://localhost:8082/data1')
      .then(function(data){
        console.log(data);      //response data1
      },function(info){
        console.log(info)
      });
    
    // 发送多个ajax请求并且保证顺序
    requestData('http://localhost:8082/data1')
            .then(function (data) {
              console.log(data)
              return requestData('http://localhost:8082/data2');
            })
            .then(function (data) {
              console.log(data);
              return requestData('http://localhost:8082/data3');
            })
            .then(function (data) {
              console.log(data);
            });
  </script>
</body>
</html>